package com.example.demo.controller;


import com.example.demo.model.Chart;
import com.example.demo.payload.DataRequest;
import com.example.demo.payload.DataResponse;
import com.example.demo.service.ChartService;
import com.example.demo.service.UserService;
import com.example.demo.utils.CommonMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/chart")
@Slf4j
public class ChartController {
    @Resource
    private ChartService chartService;
    @Resource
    private UserService userService;


    @PostMapping("/add")
    public DataResponse addChart(@RequestBody DataRequest dataRequest){
        if (dataRequest == null) {
            return CommonMethod.getReturnData(400,"参数错误");
        }
        Chart chart=new Chart();
        Date now=new Date();
        chart.setName(dataRequest.getString("chartName"));
        chart.setChartData(dataRequest.getString("chartData"));
        chart.setChartType(dataRequest.getString("chartType"));
        chart.setGenResult(dataRequest.getString("genResult"));
        chart.setGoal(dataRequest.getString("goal"));
        chart.setChartType(dataRequest.getString("chartType"));
        chart.setCreateTime(now);
        chart.setUpdateTime(now);
        String userAccount=dataRequest.getString("userAccount");
        int userId= userService.GetUserId(userAccount);
        chart.setUserId(userId);
        chart.setIsDelete(dataRequest.getInteger("isDelete"));
        boolean result=chartService.save(chart);
        int newChartId = chart.getId();
        Map data =new HashMap();
        data.put("chartId",newChartId);
        if(result){
            return CommonMethod.getReturnData(200,data,"创建成功");
        }
        return CommonMethod.getReturnMessage(400,"创建失败");
    }

    @PostMapping("/getLastChart")
    public DataResponse getLastChart(@RequestBody DataRequest dataRequest){
        String userAccount= dataRequest.getString("userAccount");
        int userId = userService.GetUserId(userAccount);
         Chart chart = chartService.GetLastChart(userId);
         return CommonMethod.getReturnData(200,chart,"ok");
    }

    @PostMapping("/getChartPagination")
    public DataResponse getChartPagination(@RequestBody DataRequest dataRequest){
        String userAccount=dataRequest.getString("userAccount");
        Integer pageNum= dataRequest.getInteger("page");
        int userId = userService.GetUserId(userAccount);
        List<Chart> data = chartService.GetChartPagination(userId,pageNum);
        return CommonMethod.getReturnData(200,data,"ok");
    }

    @PostMapping("/getUserChartNum")
    public DataResponse getUseChartNum(@RequestBody DataRequest dataRequest){
        String userAccount=dataRequest.getString("userAccount");
        int userId= userService.GetUserId(userAccount);
        Long total = chartService.GetUserChartNum(userId);
        Map data=new HashMap();
        data.put("totalCount",total);
        return CommonMethod.getReturnData(200,data,"ok");
    }

    @PostMapping("/deleteChartById")
    public DataResponse deleteChartById(@RequestBody DataRequest dataRequest){
        int chartId=dataRequest.getInteger("chartId");
        int result=chartService.DeleteChartById(chartId);
        return CommonMethod.getReturnData(200,result,"ok");
    }

    @PostMapping("/queryByChartName")
    public DataResponse queryByChartName(@RequestBody DataRequest dataRequest){
        String userAccount=dataRequest.getString("userAccount");
        int userId=userService.GetUserId(userAccount);
        String chartName= dataRequest.getString("chartName");
        List<Chart> data=chartService.queryByChartName(userId,chartName);
        return CommonMethod.getReturnData(200,data,"查找成功");
    }

    @PostMapping("/queryByChartId")
    public DataResponse queryByChartId(@RequestBody DataRequest dataRequest){
        int chartId= dataRequest.getInteger("chartId");
        Chart data= chartService.queryChartByChartId(chartId);
        return CommonMethod.getReturnData(200,data,"查找成功");
    }

    @PostMapping("/updateChartData")
    public DataResponse updateChartData(@RequestBody DataRequest dataRequest){
        int chartId= dataRequest.getInteger("chartId");
        String chartData= dataRequest.getString("chartData");
        int data=chartService.updateChartData(chartId,chartData);
        return CommonMethod.getReturnData(200,data,"ok!");
    }

    @PostMapping("/updateChartInfo")
    public DataResponse updateChartInfo(@RequestBody DataRequest dataRequest){
        int chartId= dataRequest.getInteger("chartId");
        String chartName= dataRequest.getString("chartName");
        String genResult= dataRequest.getString("genResult");
        int data=chartService.updateChartInfo(chartId,chartName,genResult);
        return CommonMethod.getReturnData(200,data,"ok!");
    }

    @PostMapping("/queryRunningChart")
    public DataResponse queryRunningChart(@RequestBody DataRequest dataRequest){
        String userAccount= dataRequest.getString("userAccount");
        int userId=userService.GetUserId(userAccount);
        String status= dataRequest.getString("status");
        List<Chart> data=chartService.queryChartByStatus(userId,status);
        return CommonMethod.getReturnData(200,data,"ok!");
    }
}
